package com.fita.admin.entity.search.agent;

import com.demo2do.core.persistence.support.QueryBuilder;
import com.demo2do.core.persistence.support.QueryFilter;
import com.demo2do.core.persistence.support.QueryOrder;
import com.fita.entity.agent.AgentType;

import java.util.Map;

/**
 * 
 * @author cuteHU
 *
 */
public class AgentCompanySearch implements QueryBuilder {

    /** 代理名称 */
    private String name;

    /** 代理类型 */
    private AgentType type;

    /** 公司名称 */
    private String companyName;

    /** 所属销售 */
    private Long principalId;

    /** 是否冻结 */
    private Boolean disabled;

    // 分区ID
    private Long districtId;

    private QueryFilter queryFilter;

    private QueryOrder queryOrder;

    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
     */
    public QueryFilter getPreparedFilter(boolean refresh) {
        if (queryFilter == null || refresh) {
            queryFilter = new QueryFilter()
                    .addLike("name", name)
                    .addLike("companyName", companyName)
                    .addEquals("disabled", disabled)
                    .addEquals("type", type)
                    .addEquals("principal.id", principalId).addEquals("district.id", districtId);
        }
        return queryFilter;
    }

    /**
     * 绑定分区ID
     * @param districtId
     * @return
     */
    public AgentCompanySearch bindDistrictId(Long districtId){
        this.districtId = districtId;
        return this;
    }

    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
     */
    public QueryOrder getOrder() {
        if (queryOrder == null) {
            queryOrder = new QueryOrder();
        }
        return queryOrder;
    }

    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
     */
    public String buildSentenceTemplate() {
        return "FROM AgentCompany WHERE disabled = false AND primaryAgent IS NULL " + getPreparedFilter(false).clauseSentence();
    }

    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
     */
    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).parameters();
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the type
     */
    public AgentType getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(AgentType type) {
        this.type = type;
    }

    /**
     * @return the companyName
     */
    public String getCompanyName() {
        return companyName;
    }

    /**
     * @param companyName the companyName to set
     */
    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    /**
     * @return the principalId
     */
    public Long getPrincipalId() {
        return principalId;
    }

    /**
     * @param principalId the principalId to set
     */
    public void setPrincipalId(Long principalId) {
        this.principalId = principalId;
    }

    /**
     * @return the disabled
     */
    public Boolean getDisabled() {
        return disabled;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(Boolean disabled) {
        this.disabled = disabled;
    }

    public Long getDistrictId() {
        return districtId;
    }

    public void setDistrictId(Long districtId) {
        this.districtId = districtId;
    }
}
